package com.ukao.cashregister.dialog;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.eventbus.WifiEvent;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.WifiAdminUtils;
import com.ukao.cashregister.widget.StateButton;
import com.ukao.cashregister.widget.StateImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.cashregister.R.id.link_speed;

/**
 * Created by ukaodev on 2017/6/2.
 */

public class WifiStrongDialogFragment extends BaseDialogFragment {


    @BindView(R.id.txt_signal_strength)
    TextView txtSignalStrength;
    @BindView(link_speed)
    TextView linkSpeed;
    @BindView(R.id.frequency)
    TextView frequency;
    @BindView(R.id.txt_security_level)
    TextView txtSecurityLevel;
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.title)
    TextView wifiName;
    @BindView(R.id.finish_btn)
    StateButton finishBtn;
    @BindView(R.id.not_save_btn)
    StateButton notSaveBtn;
    private Unbinder unbinder;
    private ScanResult mScanResult;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mScanResult = getArguments().getParcelable(ARG_PARAM1);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_wifi_strong_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        fillData();
        return view;
    }

    private void fillData() {
        WifiManager mWifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
        if (!CheckUtils.isNull(mScanResult)) {
            wifiName.setText(mScanResult.SSID);
            txtSignalStrength.setText(WifiAdminUtils.singlLevToStr(mScanResult.level));
            txtSecurityLevel.setText(mScanResult.capabilities);
            frequency.setText( (mScanResult.frequency/100)+ "GHz");
            linkSpeed.setText(mWifiInfo.getLinkSpeed()+"Mbps");

        }
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }



    @OnClick({R.id.finish_btn, R.id.not_save_btn,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.finish_btn:
                dismiss();

                break;
            case  R.id.close_btn:
                dismiss();
                break;


            case R.id.not_save_btn:
                if (!CheckUtils.isNull(mScanResult)){
                    WifiEvent.postHasData(WifiEvent.Message.notSave,mScanResult);
                }
                dismiss();

                break;
        }
    }



}
